<?php
/**
 * Created by PhpStorm.
 * User:tsingStar
 * Date: 2019/7/13
 * Time: 11:11
 */

namespace app\home\controller;

use app\common\model\Settings;

class Goods extends Common
{
    protected function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 热卖商品
     */
    public function hotIndex()
    {
        $this->templateTitle("热卖商品");
        $this->assign("cart_num", \app\common\model\Cart::getCartNums(session("user_id")));
        if(request()->isAjax()){
            $page = input("page");
            $pageNum = input("pageNum");
            $sort_key = input("sort_key");
            $sort_type = input("sort_type");
            $list = \app\common\model\Goods::getHotList(0, $page, $pageNum, $sort_key, $sort_type);
            $this->ajaxSuccess("请求成功", $list);
        }
        return $this->fetch('hotIndex');
    }
    /**
     * 搜索商品
     */
    public function search()
    {
        $this->templateTitle("查询结果");
        $this->assign("cart_num", \app\common\model\Cart::getCartNums(session("user_id")));
        if(request()->isAjax()){
            $page = input("page");
            $pageNum = input("pageNum");
            $sort_key = input("sort_key");
            $sort_type = input("sort_type");
            $keywords = input("keywords");
            $list = \app\common\model\Goods::getGoodsByCondition($keywords, 0, 0, $sort_key, $sort_type, true, $page, $pageNum);
            $this->ajaxSuccess("请求成功", $list);
        }
        $this->assign("keywords", input("keywords"));
        return $this->fetch('search');
    }

    /**
     * 获取商品规格
     */
    public function getOptions()
    {
        $good_id = input("good_id");
        try{
            $specs = \app\common\model\Goods::getSpec($good_id);
            $this->ajaxSuccess("请求成功", $specs);
        }catch (\Exception $e){
            $this->ajaxError($e->getMessage());
        }
    }

    /**
     * 商品详情
     */
    public function detail()
    {
        $this->templateTitle("商品详情");
        $good_id = input("good_id");
        try{
            $good = \app\common\model\Goods::check($good_id);
            //获取商品轮播图
            $good["swiper"] = \app\common\model\Goods::getSwiper($good_id);
            //获取相关商品
            $good["relative_list"] = \app\common\model\Goods::getRelative($good_id);
            //获取热门推荐
            $good["recommend_list"] = \app\common\model\Goods::getRecommend($good_id);
            //获取商品评价
            $good["comment"] = \app\common\model\Goods::getComment($good_id);
            //商品收藏状态
            $good["collect"] = \app\common\model\Goods::checkCollect($good_id);
            //商品详情
            $good["detail"] = db("goods_detail")->where("id", $good["detail_id"])->value("detail");
            //获取客服电话
            $system_setting = Settings::getSettings("key_global");
            $this->assign("server_tel", $system_setting["server_tel"]);
            $this->assign("good", $good);
        }catch (\Exception $e){
            exit($e->getMessage());
        }
        $this->assign("info", $good);
        return $this->fetch("detail");
    }

    /**
     * 获取商品评论
     */
    public function getComment($good_id, $page, $page_num)
    {
        $comment = \app\common\model\Goods::getComment($good_id, $page, $page_num);
        foreach ($comment as &$c){
            $c["comment_time"] = date("Y-m-d H:i:s", $c["comment_time"]);
        }
        $this->ajaxSuccess("请求成功", $comment);
    }

    /**
     * 商品列表
     */
    public function index()
    {
        $this->templateTitle("商品列表");
        if(request()->isAjax()){
            $keywords = input("post.keywords")?:"";
            $brand_id = input("post.brand_id")?:0;
            $cate_id = input("post.cate_id")?:0;
            $sort_key = input("post.sort_key");
            $sort_type = input("post.sort_type");
            $presell_id = input("post.presell_id");
            $page = input("page");
            $page_num = input("page_num");
            $good_list = \app\common\model\Goods::getGoodsByCondition($keywords, $cate_id, $brand_id, $sort_key, $sort_type, true, $page, $page_num, $presell_id);
            $this->ajaxSuccess("请求成功", $good_list);
        }
        //预售商品
        $presell_id = input("presell_id")?:0;
        $this->assign("presell_id", $presell_id);
        //商品分类
        $cate_id = input("cate_id");
        $this->assign("cate_id", $cate_id);
        //品牌id
        $brand_id = input("brand_id");
        $this->assign("brand_id", $brand_id);
        $keywords = input("keywords");
        $this->assign("keywords", $keywords);
        //获取商品分类信息
        $cate_list = \app\common\model\Category::getCategory();
        foreach ($cate_list as &$item){
            $item['children'] = \app\common\model\Category::getCategory($item["id"]);
        }
        $this->assign("cate_list", $cate_list);
        //获取品牌信息
        $brand_list = db("brand")->where("is_valid", 1)->where("is_visible", 1)->field("id, title")->select();
        $this->assign("brand_list", $brand_list);
        return $this->fetch("index");
    }

    /**
     * 添加收藏
     */
    public function addCollect($good_id, $type)
    {
        try{
            if($type == 1){
                //删除收藏
                db("goods_collection")->where("goods_id", $good_id)->where("uid", session("user_id"))->delete();
            }else{
                db("goods_collection")->insert([
                    "uid"=>session("user_id"),
                    "goods_id"=>$good_id,
                    "create_time"=>time()
                ]);
            }
            $this->ajaxSuccess("操作成功");
        }catch (\Exception $e){
            $this->ajaxError("操作失败");
        }
    }


}